package com.it2297.view;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.TitledBorder;

import com.alee.extended.image.WebImage;
import com.it2297.component.AbstractPagination;
import com.it2297.main.App;

@SuppressWarnings("serial")
public class UsedItemDetailDialog extends JDialog
{
	private int id;
	private final JPanel canvas = new JPanel();
	private final JPanel panel = new JPanel();
	private final JButton btnBid = new JButton("Bid");
	private final JButton btnClose = new JButton("Close");
	private final JPanel panel_1 = new JPanel();
	private final JScrollPane scrollPane = new JScrollPane();
	private final WebImage webImage = new WebImage();

	private AbstractPagination pagination;
	private final JTextPane txtpnInformation = new JTextPane();

	public UsedItemDetailDialog()
	{
		initGUI();
	}

	public void setInformation(String information)
	{
		txtpnInformation.setText(information);
	}

	public void setImage(BufferedImage bimg)
	{
		webImage.setImage(bimg);
	}

	public void setPagination(AbstractPagination pagination)
	{
		if (this.pagination != null)
		{
			this.remove(this.pagination);
			this.pagination = null;
		}
		this.pagination = pagination;
		canvas.add(pagination, BorderLayout.SOUTH);
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public int getId()
	{
		return id;
	}

	private void initGUI()
	{
		setBounds(100, 100, 800, 600);
		setLocationRelativeTo(null);
		setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
		getContentPane().setLayout(new BorderLayout(0, 0));
		canvas.setBorder(new TitledBorder(null, "Images", TitledBorder.LEFT, TitledBorder.TOP, null, null));
		canvas.setPreferredSize(new Dimension(10, 200));

		getContentPane().add(canvas, BorderLayout.CENTER);
		canvas.setLayout(new BorderLayout(0, 0));

		canvas.add(webImage);
		panel_1.setOpaque(false);
		panel_1.setFocusable(false);

		getContentPane().add(panel_1, BorderLayout.EAST);
		scrollPane.getViewport().setOpaque(false);
		panel_1.setLayout(new BorderLayout(0, 0));
		scrollPane.setViewportBorder(new TitledBorder(null, "Information", TitledBorder.LEADING, TitledBorder.TOP, null, null));

		scrollPane.setFocusable(false);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

		panel_1.add(scrollPane);
		txtpnInformation.setContentType("text/html");
		txtpnInformation.setPreferredSize(new Dimension(250, 19));

		scrollPane.setViewportView(txtpnInformation);
		FlowLayout flowLayout = (FlowLayout) panel.getLayout();
		flowLayout.setAlignment(FlowLayout.RIGHT);
		getContentPane().add(panel, BorderLayout.SOUTH);

		panel.add(btnClose);
	}

	public void addBidActionListener(ActionListener actionListener)
	{
		btnBid.addActionListener(actionListener);
	}

	public void addCloseActionListener(ActionListener actionListener)
	{
		btnClose.addActionListener(actionListener);
	}

	public void setPaginationCount(int paginationCount)
	{
		pagination.setCount(paginationCount);
	}

	public void setPaginationTotal(int paginationTotal)
	{
		pagination.setTotal(paginationTotal);
	}

	public void refresh()
	{
		pagination.refresh();
	}

	public void addBidButton()
	{
		panel.add(btnBid);
	}

}
